//
//  ZXTabBarController.m
//  ZXOCWeiBo
//
//  Created by mac on 15/11/28.
//  Copyright © 2015年 itcast. All rights reserved.
//

#import "ZXTabBarController.h"
/**
 *  我
 */
#import "ZXMeViewController.h"
/**
 *  消息
 */
#import "ZXMessageViewController.h"
/**
 *  主页
 */
#import "ZXHomeViewController.h"
/**
 *  发现
 */
#import "ZXDiscoverViewController.h"

#import "ZXNavigationController.h"
/**
 *  发微博的控制器
 */
#import "ZXComposeController.h"
//#import "ZXTabbar.h"

#import "ZXUnreadCountTool.h"

#import "ZXUnreadCountParam.h"
#import "ZXUnreadCountResult.h"


@interface ZXTabBarController ()

/**
 *  加号按钮
 */
@property(nonatomic,strong)UIButton* plusBtn;

@property(nonatomic,strong)UITabBarItem* currentItem;

@property(nonatomic,strong)ZXHomeViewController* homeVc;

@end

@implementation ZXTabBarController

- (void)viewDidLoad {
    [super viewDidLoad];
    //添加子控制器（导航控制器）
    [self setupChildControllers];
    
//    ZXTabbar * tabbar = [[ZXTabbar alloc]init];
//    //使用kVC方式可替换系统的tabbar 如果类有个readonly修饰的属性可以使用KVC替换掉
//    [self setValue:tabbar forKey:@"tabBar"];
    
//    写一个定时器，发送未读消息请求
 NSTimer * timer = [NSTimer scheduledTimerWithTimeInterval:60 target:self selector:@selector(unreadCount) userInfo:nil repeats:YES];
//    定时器，要执行，要添加到主运行循环，默认NSTimer就会添加到主运行循环
    [[NSRunLoop mainRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
}
//定时器调用的方法
-(void)unreadCount
{
    NSString * url = @"https://rm.api.weibo.com/2/remind/unread_count.json";
    ZXUnreadCountParam * param = [ZXUnreadCountParam param];
    param.uid = [ZXAccount shareAccount].uid;
    
    [ZXUnreadCountTool unreadCountWithUrl:url params:param success:^(ZXUnreadCountResult *result) {
//        设置首页的未读消息
        [self setupBadgeWithIndex:0 badge:result.status];
//        设置信息的未读数

        [self setupBadgeWithIndex:1 badge:result.msgCount];
//        设置我的未读数
        [self setupBadgeWithIndex:3 badge:result.follower];
//        设置appicon 的badge
        [UIApplication sharedApplication].applicationIconBadgeNumber = result.total;
        
    } failure:^(NSError *error) {
        ZXLog(@"%@",error);
    }];
}

#pragma mark 设置tabbarItem的badge
-(void)setupBadgeWithIndex:(NSInteger)index badge:(NSInteger)badge
{
    UIViewController * nav = self.viewControllers[index];
    if (badge > 0)
    {
        nav.tabBarItem.badgeValue = [NSString stringWithFormat:@"%ld",badge];
    }
    else
    {
        nav.tabBarItem.badgeValue = nil;
    }
}
-(void)setupChildControllers
{
    //设置标题的颜色
    //    self.tabBar.tintColor = [UIColor orangeColor];
    
    //首页
    ZXHomeViewController * homeVc = [[ZXHomeViewController alloc]init];
    [self addChildVCWith:homeVc title:@"首页" nmalImageName:@"tabbar_home" selImageName:@"tabbar_home_selected"];
    self.homeVc = homeVc;
    
    //消息
    ZXMessageViewController *messageVc = [[ZXMessageViewController alloc]init];
    [self addChildVCWith:messageVc title:@"消息" nmalImageName:@"tabbar_message_center" selImageName:@"tabbar_message_center_selected"];
    
    //添加一个占位控制器
        UIViewController * Vc = [[UIViewController alloc]init];
        [self addChildVCWith:Vc title:nil nmalImageName:@"aa" selImageName:@"aa"];
    
    //发现
    ZXDiscoverViewController * discoverVc = [[ZXDiscoverViewController alloc]init];
    [self addChildVCWith:discoverVc title:@"发现" nmalImageName:@"tabbar_discover" selImageName:@"tabbar_discover_selected"];
    
    //我
    ZXMeViewController * meVc= [[ZXMeViewController alloc]init];
    [self addChildVCWith:meVc title:@"我" nmalImageName:@"tabbar_profile" selImageName:@"tabbar_profile_selected"];
}

/**
 *  添加tabBar的子控制器
 *
 *  @param vc            子控制器
 *  @param title         子控制器在tabbar中的标题
 *  @param nmalImageName 普通状态下的图片
 *  @param selImageName  高亮状态下的图片
 */
-(void)addChildVCWith:(UIViewController *)vc title:(NSString *)title nmalImageName:(NSString *)nmalImageName selImageName:(NSString *)selImageName
{
    //给控制器包装
    ZXNavigationController * nav = [[ZXNavigationController alloc]initWithRootViewController:vc];
//    NSLog(<#NSString * _Nonnull format, ...#>)
    
    //添加子控制器
    [self addChildViewController:nav];
    //设置控制器标题
    nav.tabBarItem.title = title;
    
    //设置控制器标题的图片普通状态下
    nav.tabBarItem.image =  [UIImage imageNamed:nmalImageName];
    
    
    //高亮状态下
    nav.tabBarItem.selectedImage = [UIImage imageNamed:selImageName];
    
    //设置图片不被渲染
    UIImage * selImg = [UIImage imageNamed:selImageName];
   selImg =  [selImg imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    nav.tabBarItem.selectedImage = selImg;
    
    [nav.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor orangeColor]} forState:UIControlStateSelected];
    
    
}
//在View即将显示的时候调用
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    CGFloat centerX = self.tabBar.w * 0.5;
    CGFloat centerY = self.tabBar.h * 0.5;
    
    //设置按钮的中心位置
    self.plusBtn.center = CGPointMake(centerX, centerY);
    
}
//监听加号按钮的点击
-(void)plusBtnClick
{
//    NSLog(@"点击了");
    ZXLog(@"点击了加号按钮");
    
    ZXComposeController * composeVc = [[ZXComposeController alloc]init];
    
    UINavigationController * nav = [[UINavigationController alloc]initWithRootViewController:composeVc];
    
    [self presentViewController:nav animated:YES completion:nil];
}
//选中了那个按钮就会调用这个方法
-(void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
{
    if ([item.title isEqual:@"首页"])
    {
        //当点击首页时，以前就是显示首页
        if (item == self.currentItem || self.currentItem == nil)
        {
            [self.homeVc refresh];
        }
    }
    
    //记录当前选中的item
    self.currentItem = item;
}

#define mark 懒加载
-(UIButton *)plusBtn
{
    if (!_plusBtn)
    {
        UIButton * plusBtn  = [[UIButton alloc]init];
        [plusBtn setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button"] forState:UIControlStateNormal];
        [plusBtn setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button_highlighted"] forState:UIControlStateHighlighted];
        [plusBtn setImage:[UIImage imageNamed:@"tabbar_compose_icon_add"] forState:UIControlStateNormal];
        [plusBtn setImage:[UIImage imageNamed:@"tabbar_compose_icon_add_highlighted"] forState:UIControlStateHighlighted];
        [plusBtn addTarget:self action:@selector(plusBtnClick) forControlEvents:UIControlEventTouchUpInside];
        
        plusBtn.size = CGSizeMake(64, 44);
        [self.tabBar addSubview:plusBtn];
        self.plusBtn = plusBtn;

    }
    return _plusBtn;
}


@end
